//
//  RPopDownLIstView.swift
//  LZStaff
//
//  Created by ray on 2018/11/19.
//  Copyright © 2018年 ray. All rights reserved.
//

import UIKit

class RPopDownListView: UIView {

    /// 背景
    private var bgView:UIView!
    
    private var fromPoint: CGPoint!
    
    var isRouting = false
    
    var dataSource:[Dictionary<String,Any>]!
    
    var selectItemHandler: (((Dictionary<String,Any>)->())?)
    
    lazy var tableView:UITableView = {
        let tableView = UITableView.init(frame: .zero, style: .plain)
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        tableView.separatorStyle = .none
        tableView.bounces = false
        tableView.separatorInset = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)
        tableView.register(UINib.init(nibName: "RPopDownListCell", bundle: nil), forCellReuseIdentifier: "RPopDownListCell")
        return tableView
    }()
    
    init(_ point: CGPoint,_ parentVC: ViewController?,_ array:[Dictionary<String,Any>]) {
        super.init(frame: RScreen)
        self.fromPoint = point
        self.dataSource = array
        bgView = UIView.init(frame: RScreen)
        bgView.backgroundColor = UIColor.black
        bgView.alpha = 0
        let tapBgView = UITapGestureRecognizer.init(target: self, action: #selector(tapBgViewAction))
        bgView.addGestureRecognizer(tapBgView)
        
        self.configTBView()
        if let tmpVC = parentVC{
            tmpVC.view.addSubview(bgView)
            tmpVC.view.addSubview(tableView)
        }else{
            currentWindow?.rootViewController?.view.addSubview(bgView)
            currentWindow?.rootViewController?.view.addSubview(tableView)
        }
        self.showView()
    }
    /// 配置tableview
    func configTBView(){
        tableView.frame = CGRect.init(x: 0, y: fromPoint.y+64, width: RScreenW, height: 50)
        tableView.delegate = self
        tableView.dataSource = self
        self.addSubview(tableView)
    }
    
    
    @objc private func cancelAction() {
        DispatchQueue.main.async {
            self.hideView()
        }
    }
    @objc func tapBgViewAction(){
        DispatchQueue.main.async {
            self.hideView()
        }
    }
    @objc private func confirmAction() {
        
        DispatchQueue.main.async {
            self.hideView()
        }
    }
    
    private func showView() {
        var height = RScreenH-64-self.fromPoint.y
        CGFloat(self.dataSource.count * 50) > height ? (height = RScreenH-64-self.fromPoint.y-20) : (height = CGFloat(self.dataSource.count * 50))
        UIView.animate(withDuration: 0.3, animations: {
            self.bgView.alpha = 0.5
            self.tableView.alpha = 1
            
            self.tableView.frame = CGRect.init(x: 0, y: self.fromPoint.y+64, width: RScreenW, height: height)
        }) { (flag) in
            
        }
    }
    @objc private func hideView() {
        
        UIView.animate(withDuration: 0.3, animations: {
            self.bgView.alpha = 0
            self.tableView.alpha = 0
            self.tableView.frame = CGRect.init(x: 0, y: self.fromPoint.y+64, width: RScreenW, height: 50)
        }) { (flag) in
            self.tableView.frame = CGRect.init(x: 0, y: self.fromPoint.y+64, width: RScreenW, height: 50)
            self.bgView.removeFromSuperview()
            self.tableView.removeFromSuperview()
            self.removeFromSuperview()
        }
    }
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}


extension RPopDownListView: UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if let handler = selectItemHandler{
            handler(self.dataSource[indexPath.row])
        }
        self.hideView()
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "RPopDownListCell", for: indexPath) as! RPopDownListCell
        cell.configModel(flag:self.isRouting ,dic: self.dataSource[indexPath.row])
        return cell
    }
}
